package com.tjvib.base;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tjvib.common.RetryCallback;
import com.tjvib.presenter.BasePresenter;
import com.tjvib.util.Manager.AppManager;
import com.tjvib.util.ToastUtil;
import com.tjvib.view.activity.BootingActivity;
import com.tjvib.view.activity.MainActivity;
import com.tjvib.view.dialog.HintDialog;

import java.util.ArrayList;

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {

    protected final String TAG = this.getClass().getSimpleName();
    //是否显示标题栏
    private boolean isShowTitle = true;
    //是否显示状态栏
    private boolean isShowStatusBar = true;
    //是否允许旋转屏幕
    private boolean isAllowScreenRoate = true;

    public T presenter;
    public ArrayList<Dialog> dialogs = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppManager.getInstance().addActivity(this);

//        if (!isShowTitle) {
//            requestWindowFeature(Window.FEATURE_NO_TITLE);
//        }
//
//        if (isShowStatusBar) {
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
//                    , WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        }

        //设置布局
        setContentView(initLayout());

        //设置屏幕是否可旋转
//        if (!isAllowScreenRoate) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        } else {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }

        presenter = genPresenter();

        if (!initView()) {
            ToastUtil.show("初始化界面失败");
            return;
        }

        if (!initData()) {
            ToastUtil.show("初始化数据失败");
            return;
        }
    }

    /**
     * 初始化布局
     *
     * @return 布局id
     */
    protected abstract int initLayout();

    /**
     * 初始化控件
     */
    protected abstract boolean initView();

    /**
     * 设置数据
     */
    protected abstract boolean initData();

    /**
     * 设置是否显示标题栏
     *
     * @param showTitle true or false
     */
    public void setShowTitle(boolean showTitle) {
        isShowTitle = showTitle;
    }

    /**
     * 设置是否显示状态栏
     *
     * @param showStatusBar true or false
     */
    public void setShowStatusBar(boolean showStatusBar) {
        isShowStatusBar = showStatusBar;
    }

    /**
     * 是否允许屏幕旋转
     *
     * @param allowScreenRoate true or false
     */
    public void setAllowScreenRoate(boolean allowScreenRoate) {
        isAllowScreenRoate = allowScreenRoate;
    }

    /**
     * 保证同一按钮在2秒内只会响应一次点击事件
     */
    public abstract class OnSingleClickListener implements View.OnClickListener {
        //两次点击按钮之间的间隔，目前为2000ms
        private static final int MIN_CLICK_DELAY_TIME = 2000;
        private long lastClickTime;

        public abstract void onSingleClick(View view);

        @Override
        public void onClick(View view) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                onSingleClick(view);
            }
        }
    }

    /**
     * 同一按钮在短时间内可重复响应点击事件
     */
    public abstract class OnMultiClickListener implements View.OnClickListener {
        public abstract void onMultiClick(View view);

        @Override
        public void onClick(View v) {
            onMultiClick(v);
        }
    }


    /**
     * 隐藏软键盘
     */
    public void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null && null != imm) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     */
    public void showSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null && null != imm) {
            imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
        }
    }


    public void showErrorDialog(String message) {
        runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(this);
            hintDialog.showDialog(this, "错误", message,
                    view -> hintDialog.dismiss(),
                    view -> hintDialog.dismiss());
        });
    }

    public void showErrorDialogAndExit(String message) {
        runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(this);
            hintDialog.showDialog(this, "错误", message,
                    view -> {
                        hintDialog.dismiss();
                        startActivity(new Intent(this, BootingActivity.class));
                        AppManager.getInstance().finishOtherActivity(BootingActivity.class);
                    },
                    view -> hintDialog.dismiss());
        });
    }

    public void showErrorDialogWithRetry(String message, RetryCallback callback) {
        runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(this);
            hintDialog.setConfirmText("重试");
            hintDialog.showDialog(this, "错误", message,
                    view -> {
                        hintDialog.dismiss();
                        callback.onRetry();
                    },
                    view -> {
                        hintDialog.dismiss();
                        AppManager.getInstance().finishActivity();
                    });
        });
    }

    public void showHintDialog(String message) {
        runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(this);
            hintDialog.showDialog(this, "提示", message,
                    view -> hintDialog.dismiss(),
                    view -> hintDialog.dismiss());
        });
    }

    public void showErrorDialogFinish(String message) {
        runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(this);
            hintDialog.showDialog(this, "错误", message,
                    view -> {
                        hintDialog.dismiss();
                        AppManager.getInstance().finishActivity();
                    },
                    view -> {
                        hintDialog.dismiss();
                        AppManager.getInstance().finishActivity();
                    });
                });
    }


    public void showHintDialogFinish(String message) {
        runOnUiThread(() -> {
            HintDialog hintDialog = new HintDialog(this);
            hintDialog.showDialog(this, "提示", message,
                    view -> {
                        hintDialog.dismiss();
                        AppManager.getInstance().finishActivity();
                    },
                    view -> {
                        hintDialog.dismiss();
                        AppManager.getInstance().finishActivity();
                    });
        });
    }


    protected abstract T genPresenter();
}
